<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-20
 * Time: 13:25
 */
namespace app\index\controller;

use think\Db;

class Branch extends Base
{
    public function index()
    {//部门列表
        $this->baseindex();
        //dump($list);die;
        $where = 'b.state=1';
        if (isset($_POST['submit']) && $_POST['submit'] == "submit") {//搜索
            $region_id = $_POST['region_id'];
            $branch_id = $_POST['branch_id'];
            $ch_name = $_POST['ch_name'];
            if ($region_id != 2){
                $where.="  and b.region_id=" . $region_id;
            }
            if ($branch_id !=1){
                $where.=" and b.id=" . $branch_id;
            }
            if (!empty($ch_name)){
                $where.=" and `u`.`ch_name` LIKE '%" . $ch_name . "%'";
            }
        }
        $list = Db::table('oa_branch b')->field('b.branch,b.id,u.ch_name,r.region')
            ->join('oa_region r', 'b.region_id=r.id', 'left')->join('oa_user u', 'b.user_id=u.id', 'left')
            ->where($where)->select();
        //dump($list);die;
        $this->assign('list', $list);

        $region_result = Db::table('oa_region')->field('id,region')->where('state=1')->select();
        $this->assign('region', $region_result);
        //$this->assign('branch', $branch);
        return $this->fetch();
    }

    public function indexlist()
    {
        $this->baseindex();
        $where = 'b.state=1';
        if (isset($_POST['data'])) {//搜索
            $res = json_decode(input('post.data'));
            $region_id = $res['region_id'];
            $branch_id = $res['branch_id'];
            $ch_name = $res['ch_name'];
            if (!empty($ch_name)) {
                $where .= " and b.region_id=" . $region_id . " and b.id=" . $branch_id . " and `u`.`ch_name` LIKE '%" . $ch_name . "%'";
            } else {
                $where .= " and b.region_id=" . $region_id . " and b.id=" . $branch_id;
            }
        }
        //dump($count);die;
        $list = Db::table('oa_branch b')->field('b.branch,b.id,u.ch_name,r.region')
            ->join('oa_region r', 'b.region_id=r.id', 'left')->join('oa_user u', 'b.user_id=u.id', 'left')
            ->where($where)->select();
        $num = count($list);
        $lists['code'] = 0;
        $lists['msg'] = "";
        $lists['count'] = $num;
        $lists['data'] = $list;
        //dump($lists);die;
        return $lists;
    }

    public function region()
    {
        $this->baseindex();
        //所属公司的部门
        if (isset($_POST['region_id'])) {
            $id = $_POST['region_id'];
            $branch = Db::table('oa_branch')->field('id,branch')->where('region_id=' . $id)->select();
            return $branch;
        }
    }

    public function branchadd()
    {//添加部门
        $this->baseindex();
        $num = '';
        if (isset($_POST['submit']) && $_POST['submit'] == "submit") {
            $res = input('post.data');
            $ress = json_decode($res, true);
            //return $ress['branch'];
            $ch_name['ch_name'] = $ress['ch_name'];
            $where['region_id'] = $ress['region_id'];
            $where['branch'] = $ress['branch'];
            $wheres = $ress['branch'];
            if (!empty($wheres)) {
                $branch = Db::table('oa_branch')->where(['branch' => $where['branch']])->where('region_id=' . $where['region_id'])->select();
                if (empty($branch)) {
                    $user_id = Db::table('oa_user')->field('id')->where($ch_name)->find();
                    if (!empty($user_id)) {
                        $where['user_id'] = $user_id['id'];
                        $num = Db::table('oa_branch')->insert($where);
                        return $num;
                    } else {
                        $where['user_id'] = 0;
                        Db::table('oa_branch')->insert($where);
                        return 3;
                    }
                } else {
                    return 2;
                }
            } else {
                return $num;
            }
        }
        $region_result = Db::table('oa_region')->field('id,region')->where('state=1')->select();
        $this->assign('region', $region_result);
        return $this->fetch();
    }

    public function branchdel()
    {//删除部门
        $this->baseindex();
        if (isset($_POST['id'])) {
            $a = $_POST['id'];
            $num = Db::table('oa_branch')->where('id=' . $a)->update(['state' => 0]);
            return json_encode($num);
        }
    }

    public function branchsave()
    {
        $this->baseindex();
        //修改部门所属公司数据展示
        if (isset($_GET['id'])) {
            $id = input('get.id');
            $region = Db::table('oa_region')->where('state=1')->select();
            $arr = DB::table('oa_branch b')->field('b.id,b.branch,b.region_id,u.ch_name')->join('oa_user u', 'b.user_id=u.id', 'left')
                ->where('b.id=' . $id)->find();
        }
        $this->assign('branch', $arr);
        $this->assign('region', $region);
        return $this->fetch();
    }

    public function branchsave_sub()
    {
        $this->baseindex();
        if (isset($_POST['sub']) && $_POST['sub'] == "修改") {//修改部门所属公司负责人
            $num = '0';
            $datas = input('post.data');
            $data = json_decode($datas, true);
            $ch_name = $data['ch_name'];
            $branch = $data['branch'];
            $region = $data['region_id'];
            $branch_id = $data['id'];
            //return $branch_id;
            //$region_id=input('post.region_id');
            if (empty($ch_name)) {
                if (!empty($branch)) {
                    $res = Db::table('oa_branch')->where(['branch' => $branch, 'region_id' => $region])->select();
                    if (count($res) == 1 && empty($res)) {
                        if ($res[0]['id'] == $branch_id) {
                            $num = DB::table('oa_branch')->where(['id' => $branch_id])->update(['branch' => $branch, 'user_id' => 0, 'region_id' => $region]);
                            return $num;
                        }
                    } else {
                        return $num;
                    }
                }
            } else {
                $user_id = Db::table('oa_user')->field('id')->where(["ch_name" => $ch_name])->find();
                if (!empty($user_id)) {
                    if (!empty($branch)) {
                        $res = Db::table('oa_branch')->where(['branch' => $branch, 'region_id' => $region])->select();
                        //return count($res);
                        if (count($res) == 1) {
                            //return $res[0]['id'];
                            if ($res[0]['id'] == $branch_id) {
                                //return $res[0]['id'];
                                DB::table('oa_branch')->where(['id' => $branch_id])->update(['branch' => $branch, 'user_id' => $user_id['id'], 'region_id' => $region]);
                                return 1;
                            } else {
                                return $num;
                            }
                        } elseif (empty($res)) {
                            $num = DB::table('oa_branch')->where(['id' => $branch_id])->update(['branch' => $branch, 'user_id' => $user_id['id'], 'region_id' => $region]);
                            return $num;
                        } else {
                            return $num;
                        }
                    } else {
                        return $num;
                    }
                } else {
                    if (!empty($branch)) {
                        $res = Db::table('oa_branch')->where(['branch' => $branch, 'region_id' => $region])->select();
                        if (count($res) == 1 && empty($res)) {
                            if ($res[0]['id'] == $branch_id) {
                                DB::table('oa_branch')->where(['id' => $branch_id])->update(['branch' => $branch, 'user_id' => 0, 'region_id' => $region]);
                                return 2;
                            } else {
                                return $num;
                            }
                        } else {
                            return $num;
                        }
                    } else {
                        return $num;
                    }
                }
            }
        }
    }

    public function aa()
    {
        $sql = "id>1";
        $sql1 = " and id<9";
        $sql = $sql . $sql1;
        $str = Db::table('oa_branch')->where($sql)->select();
        //$str1=;
        $res = $str;
        dump($str);
        die;
    }
}